package com.net.xpay.web.controller.common;

import com.google.common.base.Strings;
import com.net.xpay.web.secutiry.CurrentOperator;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.util.NetUtil;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.service.CheckCodeService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.web.secutiry.Operator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
  on 08/02/2018.
 */
@RestController
@RequestMapping("/common/check-code")
@Api(tags = "公共/验证码", description = "公共 验证码 net")
@Slf4j
public class CheckCodeController {

    @Autowired
    private CheckCodeService checkCodeService;
    @Autowired
    private UserManager userManager;


    @ApiOperation("获取注册验证码")
    @GetMapping("/register")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getRegisterCheckCode(HttpServletRequest request, String mobile) {
        if (Strings.isNullOrEmpty(mobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        User user = userManager.findByMobile(mobile);
        if (user != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "该手机号已注册");
        }

        return new ResultData<>(sendCheckCodeForMobile(request, mobile, CheckCodeType.REGISTER));
    }

    @ApiOperation("获取登录验证码")
    @GetMapping("/login")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getLoginCheckCode(HttpServletRequest request, String mobile) {
        if (Strings.isNullOrEmpty(mobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        User user = userManager.findByMobile(mobile);
        if (user == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "该手机号尚未注册");
        }
        return new ResultData<>(sendCheckCodeForMobile(request, mobile, CheckCodeType.LOGIN));
    }


    @ApiOperation("获取忘记密码验证码")
    @GetMapping("/reset-password")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getResetPasswordCheckCode(HttpServletRequest request, String mobile) {
        if (Strings.isNullOrEmpty(mobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        User user = userManager.findByMobile(mobile);
        if (user == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "该手机号尚未注册");
        }

        sendCheckCodeForMobile(request, mobile, CheckCodeType.RESET_PASSWORD);

        return new ResultData<>();
    }

    @ApiOperation("获取修改密码验证码")
    @GetMapping("/modify-password")
    public ResultData<Boolean> getModifyPasswordCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.MODIFY_PASSWORD));
    }

    @ApiOperation("获取谷歌验证器验证码")
    @GetMapping("/get-google-auth")
    public ResultData<Boolean> getGoogleAuthCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.GOOGLE_AUTH));
    }

    @ApiOperation("获取修改支付密码验证码")
    @GetMapping("/modify-pay-password")
    public ResultData<Boolean> getModifyPayPasswordCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.MODIFY_PAY_PASSWORD));
    }

    @ApiOperation("获取修改手机号验证码")
    @GetMapping("/modify-mobile")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getRegisterCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.MODIFY_MOBILE));
    }

    @ApiOperation("获取修改ip白名单验证码")
    @GetMapping("/modify-white-ip")
    public ResultData<Boolean> getModifyWhiteIpCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.MODIFY_WHITE_IP));
    }

    @ApiOperation("获取用户提现验证码")
    @GetMapping("/user-withdraw")
    public ResultData<Boolean> getUserWithdrawCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.WITHDRAW));
    }

    @ApiOperation("获取修改推送地址验证码")
    @GetMapping("/modify-notify-url")
    public ResultData<Boolean> getModifyNotifyUrlCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.MODIFY_NOTIFY_URL));
    }

    @ApiOperation("获取修改安全付推送地址验证码")
    @GetMapping("/modify-safe-pay-notify-url")
    public ResultData<Boolean> getModifySafePayNotifyUrlCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.MODIFY_SAFE_PAY_NOTIFY_URL));
    }

    @ApiOperation("获取开发设置验证码")
    @GetMapping("/get-develop-settings")
    public ResultData<Boolean> getDevelopSettingsCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator, request, CheckCodeType.DEVELOP_SETTINGS));
    }


    private boolean sendCheckCodeForCurrentUser(Operator operator, HttpServletRequest request, CheckCodeType checkCodeType) {
        if (operator == null) {
            throw new BusinessException(PoseidonErrorCode.NEED_LOGIN, "用户未登陆");
        }
        User user = userManager.getById(operator.getUserId());
        return sendCheckCodeForMobile(request, user.getMobile(), checkCodeType);
    }

    private boolean sendCheckCodeForMobile(HttpServletRequest request, String mobile, CheckCodeType checkCodeType) {
        CheckCode checkCode = checkCodeService.sendCheckCode(mobile, checkCodeType, NetUtil.getRequestRealIp(request));
        return checkCode != null && !Strings.isNullOrEmpty(checkCode.getCode());
    }

    @ApiOperation("获取企业实名认证验证码")
    @GetMapping("/user-business-auth")
    public ResultData<Boolean> getUserBusinessAuthCheckCode(@ApiIgnore @CurrentOperator Operator operator,HttpServletRequest request, String mobile) {
        if (operator == null) {
            throw new BusinessException(PoseidonErrorCode.NEED_LOGIN, "用户未登陆");
        }
        if (Strings.isNullOrEmpty(mobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        sendCheckCodeForMobile(request,mobile,CheckCodeType.BUSINESS_AUTH);

        return new ResultData<>(true);
    }
}
